#!/usr/bin/python2.7
#coding=utf8

import xlrd,sys
from ana_excel.common.excel_col_transfer import transfer
from ana_excel.common.excel_col_transfer import add_a_month

def getvalue(index, dec_fields):
    # print(index.split(','))
    src_field_list = dec_fields[0].split(",")
    data = xlrd.open_workbook(src_field_list[0])#打开文件
    tables = data.sheets()#获取sheets
    if src_field_list[1] == "sheet1":
        table = tables[0]
    else :
        table = data.sheet_by_name(src_field_list[1])
    if index.split(',')[1] == "-1":
        row_str = src_field_list[3].split(":")
        # print(row_str)
        data = []
        row_start = int(row_str[0])-1
        row_end = int(row_str[1])-1
        while row_start <= row_end:
            date_1 = str(table.cell(row_start, 0).value)
            date_2 = str(table.cell(row_start, 1).value).replace('.','')
            # print(type(date_1))
            # print(type(date_2))
            # print(date_1)
            # print(date_2)
            date = date_1[0:4] + date_2
            date = str(date)
            # print(date)
            # sys.exit(0)
            value = table.cell(row_start, int(transfer(src_field_list[4]))).value
            data.append({date:value})
            row_start = row_start + 1
        return data
    elif index.split(',')[1] == "-2":
        col_str = src_field_list[4].split(":")
        # print(row_str)
        data = []
        row_start = int(transfer(col_str[0]))
        row_end = int(transfer(col_str[1]))
        date = src_field_list[2]
        # print(row_end)
        # print(int(src_field_list[3]),'xx')
        while row_start <= row_end:
            # print(row_start)
            # print(type(date_1))
            # print(type(date_2))
            # print(date_1)
            # print(date_2)
            # print(date)
            # sys.exit(0)
            value = table.cell(int(src_field_list[3])-1, row_start).value
            data.append({date: value})
            row_start = row_start + 1
            date = add_a_month(date)
        return data
    else:
        # print(dec_fields)
        cell_A1 = table.cell(int(src_field_list[3])-1, int(transfer(src_field_list[4]))).value
        return cell_A1


if __name__ == "__main__":
    getvalue()